{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=613
  Verdict: Accepted
}
var n,i,max,cur,j,prev:integer; a,b,res:array[0..10001] of longint;
begin
assign(input,'input.txt');
assign(output,'output.txt');
reset(input);
rewrite(output);
readln(n);
fillchar(b,sizeof(b),0);
for i:=1 to n do read(a[i]);
b[1]:=1;
max:=1;
for i:=2 to n do begin
    j:=i-1;
    cur:=-1;
    for j:=i-1 downto 1 do begin
        if (cur=-1) and (a[j]<a[i]) then cur:=j else
        if (a[j]<a[i]) and (b[j]>b[cur]) then cur:=j;
    end;
    if cur=-1 then inc(cur);
    b[i]:=b[cur]+1;
    if b[max]<b[i] then max:=i;
end;
writeln(b[max]);
cur:=b[max]-1;
prev:=max;
res[b[max]]:=a[max];
for i:=max downto 1 do begin
    if (cur>0) and (b[i]=cur) and (a[i]<a[prev]) then begin
       prev:=i;
       res[cur]:=a[i];
       dec(cur);
    end;
end;
for i:=1 to b[max] do write(res[i],' ');
end.